def convert(csv_file_name: str, libsvm_file_name: str):
    csv_file = open(csv_file_name, mode='r', encoding="utf-8")
    libsvm_file = open(libsvm_file_name, mode='w', encoding='utf-8')
    csv_file.readline()
    for line in csv_file:
        vals = line.strip().split(',')
        label = vals[-1] if vals[-1]!='' else '0'
        libsvm_file.write(label)
        for i, val in enumerate(vals[:-1]):
            libsvm_file.write(' ' + str(i+1) + ':' + val)
        libsvm_file.write('\n')
    csv_file.close()
    libsvm_file.close()

if __name__ == '__main__':
    csv_file_name = "../resources/data_format1/data_format2/test_format1.csv"
    libsvm_file_name = "../resources/data_format1/data_format2/test_format1.txt"
    convert(csv_file_name, libsvm_file_name)
